package com.tsystems.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

import com.tsystems.entities.Passenger;



public class PassengerDAO extends AbstractDAO<Passenger> {

	public Passenger getPassenger(String login) {
		
		EntityManager em = TrainsEntityManager.getInstance();	
		CriteriaBuilder builder = em.getCriteriaBuilder();
		CriteriaQuery<Passenger> criteria = builder.createQuery(Passenger.class);
		
		Root<Passenger> passenger = criteria.from(Passenger.class);
		criteria.select(passenger);
		criteria.where(builder.equal(passenger.get("login"), login));
		List<Passenger> result = em.createQuery(criteria).getResultList();
		
		return !result.isEmpty() ? result.get(0) : null;
	}
	
	public List<Passenger> getPassengerList() {
		
		EntityManager em = TrainsEntityManager.getInstance();	
		CriteriaBuilder builder = em.getCriteriaBuilder();
		CriteriaQuery<Passenger> criteria = builder.createQuery(Passenger.class);
		
		Root<Passenger> passenger = criteria.from(Passenger.class);
		criteria.select(passenger);
		
		List<Passenger> result = em.createQuery(criteria).getResultList();
		
		return result;
		

	}
	
		
	
}
